//
//  ASAlertViewController.m
//  ASAlertViewController
//
//  Created by 廖伟魏 on 16/8/25.
//  Copyright © 2016年 廖伟魏. All rights reserved.
//

#import "ASAlertViewController.h"



@implementation ASAlertViewController

+ (void)getAlertControllerWithTitle:(NSString *)title
                            message:(NSString *)message
                          sureTitle:(NSString *)sureTitle
                        cancelTitle:(NSString *)cancelTitle
                     viewController:(UIViewController *)aViewController
                         sureAction:(SureAction)aSureAction
                       cancelAction:(CancelAction)aCancelAction {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:aSureAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:aCancelAction];
    
    [alertController addAction:sureAction];
    [alertController addAction:cancelAction];
    
    [aViewController presentViewController:alertController animated:YES completion:nil];
}

+ (void)getTipAlertControllerWithTitle:(NSString *)title
                               message:(NSString *)message
                           cancelTitle:(NSString *)cancelTitle
                        viewController:(UIViewController *)aViewController
                           cancelBlock:(CancelAction)aCancelAction {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:aCancelAction];
    
    [alertController addAction:cancelAction];
    [aViewController presentViewController:alertController animated:YES completion:nil];
}

@end

@implementation ASSheetViewController

+ (void)setSheetControllerWithTipTitle:(NSString *)tipTitle message:(NSString *)message itemsArray:(NSArray *)itemsArray cancelTitle:(NSString *)cancelTitle viewController:(UIViewController *)aViewController handler:(SheetActionAtIndex)block {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:tipTitle message:message preferredStyle:UIAlertControllerStyleActionSheet];
    
    if (cancelTitle) {
        UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (block) {
                block(0);
            }
            [alertC dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertC addAction:cancelAction];
    }
    if (![itemsArray isKindOfClass:[NSNull class]] && itemsArray != nil && itemsArray.count) {
        for (int i = 0; i < itemsArray.count; i++) {
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:itemsArray[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (cancelTitle) {
                    if (block) {
                        block(i+1);
                    }
                } else {
                    if (block) {
                        block(i);
                    }
                }                
                [alertC dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertC addAction:otherAction];
        }
    }
    [aViewController presentViewController:alertC animated:YES completion:nil];
}


@end

